<template>
    <div>
        <div>
            <el-form ref="searchForm" :inline="true" :model="condition.search" label-suffix="" label-width="100px"
                     label-position="left" size="small">
                <el-row>
                    <el-col :span=6>
                        <el-form-item label="活动名称" prop="name">
                            <el-input v-model="condition.search.name"
                                      placeholder="活动名称"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col>
                        <el-form-item>
                            <el-button type="primary" v-on:click="getData">查询</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" v-on:click="showAddDialog">新增</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" v-on:click="reset">重置</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" v-on:click="exportExcel">导出</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <el-table
            id="mainTable"
            :data="tableData"
            size="small"
            header-cell-class-name="tableHeaderStyle"
            :row-class-name="tableRowClassName">
            <el-table-column label="序号" width="50">
                <template v-slot="scope">
                    <span>{{ (condition.page - 1) * condition.rows + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="name"
                label="活动名称">
            </el-table-column>
            <el-table-column
                prop="memberCardTitle"
                label="会员卡">
            </el-table-column>
            <el-table-column
                prop="appName"
                label="小程序">
            </el-table-column>
            <el-table-column
                prop="couponTitle"
                label="优惠券">
            </el-table-column>
            <el-table-column
                prop="effectiveDay"
                label="几天有效">
            </el-table-column>
            <el-table-column
                prop="effectiveTime"
                label="实际有效期">
            </el-table-column>
            <el-table-column
                prop="sendTo"
                :formatter="formatSendTo"
                label="发送对象">
            </el-table-column>
            <el-table-column
                prop="summary"
                label="总结">
                <template v-slot="scope">
                    <table-popover :text=scope.row.summary :length="16"></table-popover>
                </template>
            </el-table-column>
            <el-table-column
                prop="remark"
                label="备注">
                <template v-slot="scope">
                    <table-popover :text=scope.row.remark :length="16"></table-popover>
                </template>
            </el-table-column>
            <el-table-column
                prop="smsPushFlag"
                :formatter="formatSmsPushFlag"
                label="是否短信推送">
            </el-table-column>
            <el-table-column
                prop="createTime"
                label="创建时间">
            </el-table-column>
            <el-table-column
                prop="updateTime"
                label="更新时间">
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                align="center"
                width="80px">
                <template v-slot="scope">
                    <div>
                        <el-button @click="onDetail(scope.row)" type="text" size="small">详情</el-button>
                        <el-button @click="onDelete(scope.row)" type="text" size="small">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            small
            :hide-on-single-page="true"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="condition.page"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size.sync="condition.rows"
            layout="sizes, prev, pager, next"
            :total.sync="condition.total">
        </el-pagination>
        <el-dialog :title=addOrUpdateDialogTitle width="30%" :visible.sync="addOrUpdateDialogVisible">
            <el-form :model="addOrUpdateForm" label-width="120px" size="small">
                <el-form-item label="活动名称">
                    <el-input v-model="addOrUpdateForm.name"
                              placeholder="活动名称"></el-input>
                </el-form-item>
                <el-form-item label="会员卡">
                    <el-select v-model="addOrUpdateForm.memberCardId" placeholder="请选择会员卡" style="width:200px;">
                        <el-option
                            v-for="item in memberCards"
                            :key="item.id"
                            :label="item.title"
                            :value="item.id">
                            {{ item.title }}
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="选择小程序">
                    <el-select v-model="addOrUpdateForm.appId" placeholder="请选择小程序" style="width:200px;">
                        <el-option
                            v-for="item in miniPrograms"
                            :key="item.appId"
                            :label="item.name"
                            :value="item.appId">
                            {{ item.name }}
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="请选择优惠券">
                    <el-select v-model="addOrUpdateForm.couponId" placeholder="请选择优惠券" style="width:200px;">
                        <el-option
                            v-for="item in coupons"
                            :key="item.id"
                            :label="item.title"
                            :value="item.id">
                            {{ item.title }}
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="发送对象">
                    <el-select v-model="addOrUpdateForm.sendTo"
                               placeholder="发送对象">
                        <el-option label="客户"
                                   value="CUSTOMER"></el-option>
                        <el-option label="会员"
                                   value="MEMBER"></el-option>
                        <el-option label="手工上传"
                                   value="MANUAL"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="几天有效">
                    <el-input v-model="addOrUpdateForm.effectiveDay"
                              placeholder="几天有效"
                              oninput="value=value.replace(/[^\d]/g,'')"></el-input>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="addOrUpdateForm.remark" type="textarea"
                              placeholder="备注"></el-input>
                    <div class="form-tips">当选者手工上传的时候，备注里面填写需要发送的手机号码，多个用逗号或者分号分隔
                    </div>
                </el-form-item>
                <el-form-item label="是否短信推送">
                    <el-switch v-model="addOrUpdateForm.smsPushFlag"></el-switch>
                </el-form-item>
                <el-form-item label="选择模版" v-if="addOrUpdateForm.smsPushFlag">
                    <el-select v-model="addOrUpdateForm.smsTemplateConfigId" @change="selectChanged"
                               placeholder="选择模版">
                        <el-option
                            v-for="item in smsTemplates"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                            {{ item.name }} | {{ item.template }}
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item :label="item.name" v-for="item in smsTemplateParams" :key="item.id">
                    <el-input v-model="addOrUpdateForm.params[item.value]" :placeholder="item.name"
                              :disabled="!item.input"></el-input>
                    <div class="form-tips">{{ item.tips }}</div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addOrUpdateDialogVisible = false" size="small">取 消</el-button>
                <el-button @click="addOrUpdateConfirm" type="primary" size="small">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    addCouponMarketing,
    deleteCouponMarketing,
    getCouponByPage,
    getCouponMarketingByPage,
    getMemberCardByPage,
    getMpInfoByPage,
    selectSmsMarketingTemplates,
    updateCouponMarketing
} from '@/api/api'
import TablePopover from "@/component/single/TablePopover";

export default {
    name: "CouponMarketing",
    components: {TablePopover},
    data() {
        return {
            // 分页搜索条件
            condition: {
                page: 1,
                rows: 10,
                total: 0,
                search: {}
            },
            // 表格数据
            tableData: [],
            // 新增或者编辑对话框显示
            addOrUpdateDialogVisible: false,
            // 标题
            addOrUpdateDialogTitle: '新增',
            // 新增或者编辑对话框表单
            addOrUpdateForm: {},
            // 选中行
            selectedRow: {},

            // 小程序列表
            miniPrograms: [],
            // 会员卡列表
            memberCards: [],
            // 优惠券列表
            coupons: [],

            // 短信模版
            smsTemplates: [],
            smsTemplateParams: [],
        }
    },
    // 这里定义需要父组件传过来的东西
    props: {
        size: {
            type: String,
            default: 'small'
        },
    },
    methods: {
        selectChanged(value) {
            let template = {};
            for (let i = 0; i < this.smsTemplates.length; i++) {
                if (this.smsTemplates[i].id === value) {
                    template = this.smsTemplates[i];
                    break;
                }
            }
            this.smsTemplateParams = JSON.parse(template.params);
        },
        // 这里可以单独设置每一行的样式
        tableRowClassName({row, rowIndex}) {
            return '';
        },
        handleSizeChange(val) {
            this.getData();
        },
        handleCurrentChange(val) {
            this.getData();
        },
        // 格式布尔值
        formatBoolean: function (row, column, cellValue) {
            if (cellValue) {
                return "是";
            } else {
                return "否";
            }
        },
        formatSendTo: function (row, column, cellValue) {
            if (cellValue === 'CUSTOMER') {
                return "客户";
            } else if (cellValue === 'MEMBER') {
                return "会员";
            } else if (cellValue === 'MANUAL') {
                return "手工上传";
            } else {
                return "未知";
            }
        },
        formatSmsPushFlag: function (row, column, cellValue) {
            if (cellValue == '0') {
                return "否";
            } else if (cellValue == '1') {
                return "是";
            } else {
                return "未知";
            }
        },
        showAddDialog: function () {
            this.getMiniPrograms();
            this.getMemberCards();
            this.getCoupons();
            this.getSmsTemplates();
            this.addOrUpdateDialogTitle = '新增';
            this.addOrUpdateDialogVisible = true;
            this.addOrUpdateForm = {};
            this.$set(this.addOrUpdateForm, "smsPushFlag", true)
            this.$set(this.addOrUpdateForm, "params", {})
        },
        showUpdateDialog: function (row) {
            this.addOrUpdateDialogTitle = '编辑';
            this.addOrUpdateDialogVisible = true;
            this.selectedRow = row;
            // 深拷贝
            this.addOrUpdateForm = Object.assign({}, row);
        },
        reset: function () {
            this.$refs['searchForm'].resetFields();
        },
        exportExcel: function () {
            this.GLOBAL.exportExcel('#mainTable', '数据.xlsx');
        },
        add: function () {
            addCouponMarketing(this.addOrUpdateForm).then(data => {
                if (data.respCode == 1000) {
                    this.addOrUpdateDialogVisible = false;
                    this.getData();
                }
            });
        },
        update: function () {
            updateCouponMarketing(this.addOrUpdateForm).then(data => {
                if (data.respCode == 1000) {
                    this.addOrUpdateDialogVisible = false;
                    this.getData();
                }
            });
        },
        onDetail: function (row) {
            this.$router.push({path: '/member/coupon/marketing/detail', query: {id: row.id}});
        },
        onDelete: function (row) {
            this.$confirm('请确认是否删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteCouponMarketing({id: row.id}).then(data => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getData();
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        addOrUpdateConfirm: function () {
            if (this.addOrUpdateDialogTitle == '新增') {
                this.add();
            } else if (this.addOrUpdateDialogTitle == '编辑') {
                this.update();
            }
        },
        // 获取优惠券列表
        getCoupons: function () {
            let condition = {
                page: 1,
                rows: 100,
                search: {
                    'status': 'NORMAL',
                }
            };
            getCouponByPage(condition).then(data => {
                this.coupons = data.body.rows;
            });
        },
        // 获取会员卡列表
        getMemberCards: function () {
            let condition = {
                page: 1,
                rows: 100,
                search: {}
            };
            getMemberCardByPage(condition).then(data => {
                this.memberCards = data.body.rows;
            });
        },
        // 获取小程序列表
        getMiniPrograms: function () {
            let ss = {
                page: 1,
                rows: 10,
                total: 0,
                search: {
                    accountType: 'MINI_PROGRAM'
                }
            };
            getMpInfoByPage(ss).then(data => {
                this.miniPrograms = data.body.rows;
            });
        },
        // 获取短信营销模版
        getSmsTemplates: function () {
            selectSmsMarketingTemplates({"couponFlag": true}).then(data => {
                this.smsTemplates = data.body;
            });
        },
        // 获取表格数据
        getData: function () {
            getCouponMarketingByPage(this.condition).then(data => {
                this.tableData = data.body.rows;
                this.condition.total = data.body.total;
            });
        },
    },
    created() {
        this.getData();
    },
}
</script>

<style scoped>
.el-input {
    width: 150px !important;
}

.el-select {
    width: 150px !important;
}
</style>
